<template>
  <el-card class="picture-search-container">
    <!-- 加载动画 -->
    <div class="loading-mask" v-if="isLoading">
      <div class="loading-spinner">
        <div class="spinner"></div>
        <p class="loading-text">搜索中，请稍候...</p>
      </div>
    </div>

    <div class="header">
      <h2>以图搜图</h2>
      <el-tag type="info" size="large">搜索ID: {{ pictureId }}</el-tag>
    </div>
    <el-divider />
    <div class="original-section">
      <h3>原图</h3>
      <el-image
        class="original-image"
        :src="originalImage.url"
        fit="contain"
        :preview-src-list="[originalImage.url]"
      />
    </div>
    <el-divider />
    <div class="result-section">
      <div class="result-header">
        <h3>搜索结果 ({{ resultImages.length }})</h3>
      </div>
      <el-row :gutter="20">
        <el-col v-for="(img, idx) in resultImages" :key="idx" :xs="24" :sm="12" :md="8" :lg="6">
          <el-card class="result-card" shadow="hover" @click="copyImageUrl(img.imgUrl)">
            <el-image :src="img.imgUrl" fit="cover" class="result-image" />
            <div class="img-title">{{ img.title }}</div>
            <div class="img-url">{{ img.imgUrl }}</div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { searchPictureAPI, getPictureByIdAPI } from '@/api/pictureAPI'

const route = useRoute()
const pictureId = route.query.pictureId || null

const originalImage = ref({})
const resultImages = ref([])
const isLoading = ref(true) // 加载状态控制

const getPictureById = async (id) => {
  const res = await getPictureByIdAPI(id)
  originalImage.value = res.data
}

const searchPicture = async (pictureId) => {
  const res = await searchPictureAPI(pictureId)
  resultImages.value = res.data
}

const copyImageUrl = async (url) => {
  try {
    await navigator.clipboard.writeText(url)
    ElMessage.success('图片地址已复制')
  } catch {
    ElMessage.error('复制失败')
  }
}

onMounted(async () => {
  try {
    // 开始加载
    isLoading.value = true
    // 先获取原图再搜索
    await getPictureById(pictureId)
    await searchPicture(pictureId)
  } catch (error) {
    console.error('搜索失败:', error)
    ElMessage.error('搜索失败，请重试')
  } finally {
    // 无论成功失败都结束加载
    isLoading.value = false
  }
})
</script>

<style scoped>
.picture-search-container {
  max-width: 1100px;
  margin: 40px auto;
  padding: 32px 24px;
  background: #fff;
  position: relative; /* 为加载动画提供定位基准 */
  min-height: 400px; /* 确保加载动画有足够空间显示 */
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.original-section,
.result-section {
  margin-top: 24px;
}

.original-image {
  width: 400px;
  height: 300px;
  border-radius: 8px;
  border: 1px solid #eee;
  box-shadow: 0 2px 8px #f0f1f2;
}

.result-header {
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.result-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: box-shadow 0.2s;
}

.result-card:hover {
  box-shadow: 0 4px 16px #d3e0f7;
}

.result-image {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 6px;
  margin-bottom: 8px;
}

.img-title {
  font-size: 16px;
  color: #333;
  margin-top: 8px;
  word-break: break-all;
  text-align: center;
}

.img-url {
  font-size: 12px;
  color: #888;
  word-break: break-all;
  margin-top: 4px;
  text-align: center;
}

/* 加载动画样式 */
.loading-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  border-radius: inherit;
  transition: opacity 0.3s ease;
}

.loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 旋转动画 */
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

.loading-text {
  color: #666;
  font-size: 16px;
  font-weight: 500;
}

/* 旋转动画定义 */
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
